﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Medical_Store_Billing.Business_Login_Layer;

namespace Medical_Store_Billing
{
    public partial class Formqlkhohang : Form
    {
        SanphamBLL m_sanpham;
        NhacungcapBLL m_ncc;
        int flag;
        public Formqlkhohang()
        {
            InitializeComponent();
        }

        private void Formqlkhohang_Load(object sender, EventArgs e)
        {
            m_sanpham = new SanphamBLL();
            m_ncc = new NhacungcapBLL();
            dataGridView1.DataSource = m_sanpham.Laydssanpham1();
            Default();
            btnLuu.Enabled = false;
            btnThem.Enabled = true;
            btnSua.Enabled = true;
            cmbmancc.DataSource = m_ncc.Laydsnhacungcap();
            cmbmancc.DisplayMember = "mancc";
            cmbmancc.ValueMember = "mancc";
        }

        private void btntimkiem_Click(object sender, EventArgs e)
        {
            m_sanpham = new SanphamBLL();
            dataGridView1.DataSource = m_sanpham.Timsp(txttimkiem.Text);
        }
        private bool CheckNull()
        {
            if (txtgia.Text == "")
            {
                MessageBox.Show("Giá ko đc rỗng");
                return false;
            }
            else if (txtmahang.Text == "")
            {
                MessageBox.Show("Tên mã hàng ko đc rỗng");
                return false;
            }
            else if (txtsoluong.Text == "")
            {
                MessageBox.Show("Số lượng ko đc rỗng");
                return false;
            }
            else if (txttenhang.Text == "")
            {
                MessageBox.Show("Tên hàng ko đc rỗng");
                return false;
            }
            
            else return true;
        }
        private void Default()
        {
            txtmahang.Enabled = false;
            txtgia.Enabled = false;
            txtsoluong.Enabled = false;
            txttenhang.Enabled = false;
            cmbmancc.Enabled = false;
            richTextBox1.Enabled = false;
        }

        private void Add()
        {
            txtmahang.Enabled = false;
            txtgia.Enabled = true;
            txtsoluong.Enabled = true;
            txttenhang.Enabled = true;
            cmbmancc.Enabled = true;
            richTextBox1.Enabled = true;
        }
        private void Edit()
        {
            txtmahang.Enabled = false;
            txtgia.Enabled = true;
            txtsoluong.Enabled = true;
            txttenhang.Enabled = true;
            cmbmancc.Enabled = true;
            richTextBox1.Enabled = true;
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dong = e.RowIndex;
            txtmahang.Text = dataGridView1.Rows[dong].Cells[0].Value.ToString();
            txttenhang.Text = dataGridView1.Rows[dong].Cells[1].Value.ToString();
            txtgia.Text = dataGridView1.Rows[dong].Cells[2].Value.ToString();
            richTextBox1.Text = dataGridView1.Rows[dong].Cells[3].Value.ToString();
            txtsoluong.Text = dataGridView1.Rows[dong].Cells[4].Value.ToString();
            cmbmancc.SelectedValue = dataGridView1.Rows[dong].Cells[5].Value.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            flag = 1;
            Add();
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
            txtgia.Text = "";
            txtmahang.Text = sinhma();
            txtsoluong.Text = "";
            txttenhang.Text = "";
            
        }
        private String sinhma()
        {
            m_sanpham = new SanphamBLL();
            int i = m_sanpham.Demsanpham();
            String s = null;
            s = (i + 1).ToString();
            if (i < 9)
            {
                return "SP00" + s;
            }
            if ((i >= 9) && (i < 99))
            {
                return "SP0" + s;
            }
            else
                return "SP" + s;

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            flag = 2;
            Edit();
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            m_sanpham = new SanphamBLL();
            if (MessageBox.Show("bạn có muốn lưu?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (CheckNull() == true)
                {
                    if (flag == 1)
                    {
                        if (m_sanpham.Themsp(txtmahang.Text,cmbmancc.SelectedValue.ToString(),float.Parse(txtgia.Text),richTextBox1.Text,Int32.Parse(txtsoluong.Text),txttenhang.Text))
                            Formqlkhohang_Load(sender, e);
                    }
                    if (flag == 2)
                    {
                        if (m_sanpham.Suasp(txtmahang.Text,cmbmancc.SelectedValue.ToString(),float.Parse(txtgia.Text),richTextBox1.Text,Int32.Parse(txtsoluong.Text),txttenhang.Text))
                            Formqlkhohang_Load(sender, e);
                    }
                }
                else
                {
                    Formqlkhohang_Load(sender, e);
                }
            }
            else
                Formqlkhohang_Load(sender, e);
        }
    }
}
